var gulp = require('gulp');
var concat = require('gulp-concat');
var jshint = require('gulp-jshint');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var header = require('gulp-header');
var minifyCss = require('gulp-minify-css');



var pkg = require('./package.json');
var fmt = require('util').format;


// js语法检查
gulp.task('jshint', function () {
    return gulp.src('./app/js/*.js')
        .pipe(jshint())
        .pipe(jshint.reporter('default'));
});


var current = new Date();
var banner = fmt('/** %s - v%s Date: %d-%d-%d %d:%d:%d */\n',
    pkg.name,pkg.version,
    current.getFullYear(),
    current.getMonth() + 1,
    current.getDate(),
    current.getHours(),
    current.getMinutes(),
    current.getSeconds());

// js文件合并压缩
gulp.task('minjs', function (){
     return gulp.src('./app/js/*.js')
        .pipe(concat('eve.js'))
        .pipe(gulp.dest('./app/dist/js'))
        .pipe(uglify({mangle: false}))
        .pipe(rename('eve.min.js'))        
        .pipe(gulp.dest('./app/dist/js'))
        .pipe(header(banner))
        .pipe(gulp.dest('./app/dist/js'))
        ;
});

var libjs = [
    './app/lib/lodash/lodash.min.js',
    './app/lib/angular/angular.min.js',
    './app/lib/angular-bootstrap/ui-bootstrap-tpls.min.js',
    './app/lib/angular-ui-router/release/angular-ui-router.min.js',
    './app/lib/angularjs-slider/dist/rzslider.min.js'
];

// lib-js文件合并压缩
gulp.task('minlibjs', function (){
    return gulp.src(libjs)
        .pipe(concat('eve-lib.js'))
        .pipe(gulp.dest('./app/dist/js'));
});

// css压缩
gulp.task('mincss', function (){
     return gulp.src('./app/css/*.css')
        .pipe(minifyCss())
        .pipe(gulp.dest('./app/dist/css'))
        ;
});

// 监视文件的变化
gulp.task('watch', function () {
    gulp.watch(['./app/js/*.js','./app/css/*.css'], ['minjs', 'mincss']);
});

// 注册缺省任务
gulp.task('default', ['minjs','mincss']);